package com.grass.p019lv.novel.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.alibaba.android.arouter.launcher.ARouter;
import com.androidsa.bttsddka.R;
import com.androidx.p013lv.base.Constance;
import com.androidx.p013lv.base.bean.UserInfo;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.http.HttpUtils;
import com.androidx.p013lv.base.http.JsonParams;
import com.androidx.p013lv.base.http.UrlManager;
import com.androidx.p013lv.base.http.callback.HttpCallback;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.p015ui.LazyFragment;
import com.androidx.p013lv.base.utils.SpUtils;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.p019lv.bean.SearchNovelHotWordsBean;
import com.grass.p019lv.databinding.ActivitySearchNovelBinding;
import com.grass.p019lv.novel.adapter.AdapterNovelSearchRecommend;
import com.grass.p019lv.novel.fragment.SearchHotNovelFragment;
import com.gyf.immersionbar.ImmersionBar;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import com.zhy.view.flowlayout.TagView;
import java.util.ArrayList;
import java.util.List;

/* renamed from: com.grass.lv.novel.activity.SearchNovelActivity */
public class SearchNovelActivity extends BaseActivity<ActivitySearchNovelBinding> {
    String HOT_LIST = "HotNovelList";
    AdapterNovelSearchRecommend adapter;
    List<LazyFragment> fragmentList = new ArrayList();
    int page = 0;
    private List<String> tabTitles = new ArrayList();
    private MyAdapter viewPagerAdapter;

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return R.layout.activity_search_novel;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        this.page = getIntent().getIntExtra("page", 0);
        ((ActivitySearchNovelBinding) this.binding).imgBack.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.View$OnClickListenerC30951 */

            public void onClick(View view) {
                SearchNovelActivity.this.finish();
            }
        });
        ((ActivitySearchNovelBinding) this.binding).tvCancel.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.View$OnClickListenerC30962 */

            public void onClick(View view) {
                SearchNovelActivity.this.finish();
            }
        });
        setHistory();
        ((ActivitySearchNovelBinding) this.binding).edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C30973 */

            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (SearchNovelActivity.this.isOnClick() || i != 3) {
                    return false;
                }
                String trim = ((ActivitySearchNovelBinding) SearchNovelActivity.this.binding).edSearch.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    return true;
                }
                SpUtils.getInstance().setNovelHistory(trim);
                ARouter.getInstance().build(Constance.SEARCH_NOVEL_RESULT_ACTIVITY).withString(Key.TXT, trim).withInt("page", SearchNovelActivity.this.page).navigation();
                return true;
            }
        });
        ((ActivitySearchNovelBinding) this.binding).gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C30984 */

            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                if (!SearchNovelActivity.this.isOnClick()) {
                    SearchNovelHotWordsBean item = SearchNovelActivity.this.adapter.getItem(i);
                    if (1 == item.getHotType()) {
                        Intent intent = new Intent(SearchNovelActivity.this, NovelLabelActivity.class);
                        intent.putExtra(Key.TITLE, item.getHotWordName());
                        intent.putExtra("tagId", item.getTagId());
                        SearchNovelActivity.this.startActivity(intent);
                    }
                    if (2 == item.getHotType()) {
                        Intent intent2 = new Intent(SearchNovelActivity.this, VoiceNovelLabelActivity.class);
                        intent2.putExtra(Key.TITLE, item.getHotWordName());
                        intent2.putExtra("tagId", item.getTagId());
                        SearchNovelActivity.this.startActivity(intent2);
                    }
                    if (3 == item.getHotType()) {
                        ARouter.getInstance().build(Constance.SEARCH_NOVEL_RESULT_ACTIVITY).withString(Key.TXT, item.getHotWordName()).navigation();
                    }
                }
            }
        });
        ((ActivitySearchNovelBinding) this.binding).ivDelete.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.View$OnClickListenerC30995 */

            public void onClick(View view) {
                SpUtils.getInstance().clearNovelHistory();
                SearchNovelActivity.this.setHistory();
            }
        });
        this.tabTitles.clear();
        this.fragmentList.clear();
        this.viewPagerAdapter = new MyAdapter(getSupportFragmentManager());
        this.tabTitles.add("成人小說");
        this.fragmentList.add(0, SearchHotNovelFragment.newInstance(1));
        this.tabTitles.add("有聲讀物");
        this.fragmentList.add(1, SearchHotNovelFragment.newInstance(2));
        ((ActivitySearchNovelBinding) this.binding).viewPager.setAdapter(this.viewPagerAdapter);
        ((ActivitySearchNovelBinding) this.binding).viewPager.setOffscreenPageLimit(2);
        ((ActivitySearchNovelBinding) this.binding).tabLayout.setupWithViewPager(((ActivitySearchNovelBinding) this.binding).viewPager);
        ((ActivitySearchNovelBinding) this.binding).viewPager.setCurrentItem(this.page);
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initData() {
        super.initData();
        this.adapter = new AdapterNovelSearchRecommend(getActivity());
        ((ActivitySearchNovelBinding) this.binding).gridView.setAdapter((ListAdapter) this.adapter);
        getHotWord();
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onResume() {
        super.onResume();
        setHistory();
        showSoftInputFromWindow();
        getUserInfo();
    }

    public void getUserInfo() {
        HttpUtils.getInsatance().post(UrlManager.getInsatance().userInfo(), JsonParams.build().commit(), new HttpCallback<BaseRes<UserInfo>>("userInfo") {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C31006 */

            public void onLvSuccess(BaseRes<UserInfo> baseRes) {
                if (baseRes.getCode() == 200) {
                    SpUtils.getInstance().setUserInfo(baseRes.getData());
                }
            }
        });
    }

    public void showSoftInputFromWindow() {
        ((ActivitySearchNovelBinding) this.binding).edSearch.setFocusable(true);
        ((ActivitySearchNovelBinding) this.binding).edSearch.setFocusableInTouchMode(true);
        ((ActivitySearchNovelBinding) this.binding).edSearch.requestFocus();
        getWindow().setSoftInputMode(5);
    }

    /* access modifiers changed from: package-private */
    public void setHistory() {
        List<String> novelHistory = SpUtils.getInstance().getNovelHistory();
        if (novelHistory == null || novelHistory.size() <= 0) {
            ((ActivitySearchNovelBinding) this.binding).history.setVisibility(8);
            ((ActivitySearchNovelBinding) this.binding).rlHistory.setVisibility(8);
            return;
        }
        ((ActivitySearchNovelBinding) this.binding).history.setVisibility(0);
        ((ActivitySearchNovelBinding) this.binding).rlHistory.setVisibility(0);
        ((ActivitySearchNovelBinding) this.binding).history.setAdapter(new TagAdapter<String>(novelHistory) {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C31017 */

            public View getView(FlowLayout flowLayout, int i, String str) {
                TextView textView = (TextView) LayoutInflater.from(flowLayout.getContext()).inflate(R.layout.item_search_novel_history, (ViewGroup) ((ActivitySearchNovelBinding) SearchNovelActivity.this.binding).history, false);
                textView.setText(str);
                return textView;
            }
        });
        ((ActivitySearchNovelBinding) this.binding).history.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C31028 */

            @Override // com.zhy.view.flowlayout.TagFlowLayout.OnTagClickListener
            public boolean onTagClick(View view, int i, FlowLayout flowLayout) {
                if (SearchNovelActivity.this.isOnClick()) {
                    return false;
                }
                String charSequence = ((TextView) ((TagView) view).getTagView()).getText().toString();
                ((ActivitySearchNovelBinding) SearchNovelActivity.this.binding).edSearch.setText(charSequence);
                ARouter.getInstance().build(Constance.SEARCH_NOVEL_RESULT_ACTIVITY).withString(Key.TXT, charSequence).navigation();
                return false;
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivitySearchNovelBinding) this.binding).toolbar).init();
    }

    public void getHotWord() {
        HttpUtils.getInsatance().get(UrlManager.getInsatance().novelHotList(), new HttpCallback<BaseRes<List<SearchNovelHotWordsBean>>>(this.HOT_LIST) {
            /* class com.grass.p019lv.novel.activity.SearchNovelActivity.C31039 */

            public void onLvSuccess(BaseRes<List<SearchNovelHotWordsBean>> baseRes) {
                if (baseRes.getCode() == 200) {
                    SearchNovelActivity.this.adapter.setList(baseRes.getData());
                } else {
                    ToastUtils.getInstance().showWeak(baseRes.getMsg());
                }
            }
        });
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        HttpUtils.getInsatance().cancelTag(this.HOT_LIST);
        super.onDestroy();
    }

    /* renamed from: com.grass.lv.novel.activity.SearchNovelActivity$MyAdapter */
    class MyAdapter extends FragmentStatePagerAdapter {
        private MyAdapter(FragmentManager fragmentManager) {
            super(fragmentManager, 1);
        }

        @Override // androidx.fragment.app.FragmentStatePagerAdapter
        public Fragment getItem(int i) {
            return SearchNovelActivity.this.fragmentList.get(i);
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public int getCount() {
            return SearchNovelActivity.this.fragmentList.size();
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public CharSequence getPageTitle(int i) {
            return (CharSequence) SearchNovelActivity.this.tabTitles.get(i);
        }
    }
}
